package com.lib.dao;

import com.lib.dao.ex.DaoException;
import com.lib.dao.ex.DeleteDaoException;
import com.lib.dao.ex.InsertDaoException;
import com.lib.dao.ex.UpdateDaoException;
import com.lib.entity.Member;
import com.lib.service.ex.ServiceException;
import com.lib.util.DBUtil;
import com.lib.util.ResultSetUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class MemberDao {
    //添加会员，
    public void insertMember(Member member) throws DaoException, InsertDaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        conn = DBUtil.getConnection();
        int count = 0;
        try {
            ps = conn.prepareStatement("INSERT INTO member (name,pwd,typeId,balance,phone,idNumber,regdate) VALUES (?,?,?,?,?,?,?)");
            ps.setString(1, member.getName());
            ps.setString(2, member.getPwd());
            ps.setInt(3, member.getTypeid());
            ps.setDouble(4, member.getBalance());
            ps.setString(5, member.getPhone());
            ps.setString(6, member.getIdNumber());
            ps.setString(7, member.getRegdate());
            count = ps.executeUpdate();
        } catch (Exception e) {
            throw new DaoException("数据库异常" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (count != 1) {
            throw new InsertDaoException("数据库异常：插入数据失败");
        }
    }

    //查找Member中的会员名；
    public Member selectMemberByName(String name) throws DaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = DBUtil.getConnection();
        Member member = null;
        try {
            ps = conn.prepareStatement("select * from member where name = ?");
            ps.setString(1, name);
            rs = ps.executeQuery();
            member = ResultSetUtil.toBean(rs, Member.class);
        } catch (Exception e) {
            throw new DaoException("数据库异常" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return member;
    }

    //查找身份证号
    public Member selectMemberByIDNumber(String idNumber) throws DaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = DBUtil.getConnection();
        Member member = null;
        try {
            ps = conn.prepareStatement("select * from member where idNumber = ?");
            ps.setString(1, idNumber);
            rs = ps.executeQuery();
            member = ResultSetUtil.toBean(rs, Member.class);
        } catch (Exception e) {
            throw new DaoException("数据库异常" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return member;
    }

    //更新会员余额
    public void updateMemberBalance(String idNumber, String balance) throws UpdateDaoException, DaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        conn = DBUtil.getConnection();
        int count = 0;
        try {
            ps = conn.prepareStatement("update member set balance = ? where idNumber = ? ");
            ps.setString(1, balance);
            ps.setString(2, idNumber);
            count = ps.executeUpdate();
        } catch (Exception e) {
            throw new DaoException("更新会员余额异常：数据库异常" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (count != 1) {
            throw new UpdateDaoException("更新会员余额异常：更新失败");
        }
    }

    //根据身份证查找余额
    public Member selectMemberBalanceByidNumber(String idNumber) throws DaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = DBUtil.getConnection();
        Member member = null;
        try {
            ps = conn.prepareStatement("select balance from member where idNumber = ?");
            ps.setString(1, idNumber);
            rs = ps.executeQuery();
            member = ResultSetUtil.toBean(rs, Member.class);
        } catch (Exception e) {
            throw new DaoException("查找余额异常：数据库异常" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return member;
    }

    //会员修改
    public void updateMember(Member member) throws ServiceException, UpdateDaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        conn = DBUtil.getConnection();
        System.out.println("-----idNumber =" + member.getIdNumber());
        int count = 0;
        try {
            ps = conn.prepareStatement("update member set typeid=?,balance = ? , phone = ? , name = ?,regdate=? ,pwd = ? where idNumber = ? ");

            ps.setInt(1, member.getTypeid());
            ps.setFloat(2, member.getBalance());
            ps.setString(3, member.getPhone());
            ps.setString(4, member.getName());
            ps.setString(5, member.getRegdate());
            ps.setString(6, member.getPwd());
            ps.setString(7, member.getIdNumber());
            count = ps.executeUpdate();
        } catch (Exception e) {
            throw new ServiceException("更新会员异常：数据库异常" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (count != 1) {
            throw new UpdateDaoException("更新会员异常：更新失败");
        }
    }

    //删除会员
    public void deleteMember(String idNumber) throws ServiceException, DeleteDaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        conn = DBUtil.getConnection();
        int count = 0;
        try {
            ps = conn.prepareStatement("delete from member where idNumber = ?");
            ps.setString(1, idNumber);
            System.out.println("dao-- idnumbwe =" + idNumber);
            count = ps.executeUpdate();
        } catch (Exception e) {
            throw new ServiceException("会员删除异常:数据库异常" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (count != 1) {
            throw new DeleteDaoException("会员删除异常：删除失败");
        }


    }

    //获取会员信息
    public List<Member> GetMemberList() throws DaoException {
        List<Member> memberList = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = DBUtil.getConnection();
        try {
            ps = conn.prepareStatement("select * from member ");
            rs = ps.executeQuery();
            memberList = ResultSetUtil.toList(rs, Member.class);
        } catch (Exception e) {
            throw new DaoException("查找会员信息失败：数据库异常" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return memberList;
    }
}
